﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace LotteryAdmin.Views
{
    public partial class GlobalConfig : UserControl
    {
        AdminService.LotteryAdminClient client = WcfProxy.GetProxy;
        public GlobalConfig()
        {
            InitializeComponent();
            client.GetGlobalConfigCompleted += new EventHandler<AdminService.GetGlobalConfigCompletedEventArgs>(client_GetGlobalConfigCompleted);
            client.SaveGloablConfigCompleted += new EventHandler<AdminService.SaveGloablConfigCompletedEventArgs>(client_SaveGloablConfigCompleted);
            client.GetGlobalConfigAsync();
            loadingProgress.IsBusy = true;
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSave_Click(this.btnSave, null);
        }

        private void client_SaveGloablConfigCompleted(object sender, AdminService.SaveGloablConfigCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            MessageBox.Show("保存成功！");
        }

        private void client_GetGlobalConfigCompleted(object sender, AdminService.GetGlobalConfigCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            gdGlobal.DataContext = e.Result;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.IsZNumber(txtMaxBetCount.Text.Trim()))
            {
                MessageBox.Show("最大投注数量必须为正整数！");
                return;
            }
            if (!Validate.IsZNumber(txtMaxBetMoney.Text.Trim()))
            {
                MessageBox.Show("最大投注金额必须为正整数！");
                return;
            }
            if (!Validate.ValidateMoney(txtMinOneBetMoney.Text.Trim()))
            {
                MessageBox.Show("最小投注金额必须是数字！");
                return;
            }
            if (!Validate.ValidateMoney(txtMaxOneBetMoney.Text.Trim()))
            {
                MessageBox.Show("最大投注金额必须是数字！");
                return;
            }
            if (!Validate.IsZNumber(txtMaxOutMoneyOneDay.Text.Trim()))
            {
                MessageBox.Show("每天提现金金额必须为正整数！");
                return;
            }
            if (!Validate.IsZNumber(txtMaxOutMoneyOneTime.Text.Trim()))
            {
                MessageBox.Show("每次最大提现金额必须为正整数！");
                return;
            }
            if (!Validate.IsZNumber(txtMaxOutMoneyOneTime.Text.Trim()))
            {
                MessageBox.Show("每次最小提现金额必须为正整数！");
                return;
            }
            if (!Validate.ValidateMoney(txtMaxBonusPct.Text.Trim()))
            {
                MessageBox.Show("返点数必须为数字！");
                return;
            }
            Dictionary<int, string> dicValues = new Dictionary<int, string>();
            dicValues.Add(0, txtMaxBetCount.Text.Trim());
            dicValues.Add(1, txtMaxBetMoney.Text.Trim());
            dicValues.Add(2, txtMinOneBetMoney.Text.Trim());
            dicValues.Add(3, txtMaxOneBetMoney.Text.Trim());
            dicValues.Add(4, txtMaxOutMoneyOneDay.Text.Trim());
            dicValues.Add(5, txtMaxOutMoneyOneTime.Text.Trim());
            dicValues.Add(6, txtMinOutMoneyOneTime.Text.Trim());
            dicValues.Add(7, txtMaxOutMoneyTimesOneD.Text.Trim());
            dicValues.Add(8, txtMaxBonusPct.Text.Trim());
            dicValues.Add(9, txtKfUrl.Text.Trim());
            dicValues.Add(10,tbMemInr.Value.ToString("F1"));
            client.SaveGloablConfigAsync(dicValues);
            loadingProgress.IsBusy = true;
        }
    }
}
